#!/usr/bin/env python3

import os
import csv
import urllib.request
import base64
import datetime


serversnum = 50
mirrorlist = [ "https://www.vpngate.net", "http://5.181.235.14:29916", "http://88.218.17.24:20261", "http://138.199.46.86:36667", "http://109.111.243.206:17579", "http://78.142.193.246:33304", "http://163.182.174.159:18358", "http://91.193.75.5:45082", "http://103.201.129.226:14684", "http://146.70.205.2:6283", "http://146.70.205.6:34299", "http://62.133.35.246:2265" ]
reqpart = "/api/iphone/"
# listurl = "http://www.vpngate.net/api/iphone/"
ovpndirdir = "~/docs/ovpn"
ovpnpath = "/tmp/profile.ovpn"
fmtstrhead = "{:^4} | {:^4} | {:^4} | {:^30} | {:^10}"
fmtstr     = "[{:>2}] |  {}  | {:>4} | {:<30} | {:^10}"
scriptdir = os.path.expanduser("~/.config/openvpn")

def save_ovpn_file(reader, index, path):
    current = reader.__next__()
    fpath = "%s/[#%02d]_[%s][P%03d]_%s.ovpn" % (path, index, current[6], int(current[3]), current[0])
    with open(fpath, 'w') as f:
#    with open("%s/[#%d][P%s path + "/[#" + str(index) + "][P:" + current[0] + ".ovpn", 'w') as f:
            f.write(base64.b64decode(current[14]).decode('ascii'))
            f.write( "script-security 2\n" )
            f.write( "up \"" + scriptdir + "/up.sh\"\n" )
            f.write( "down \"" + scriptdir + "/down.sh\"\n" )
            f.write( "data-ciphers 'AES-128-CBC:AES-256-GCM:AES-128-GCM:CHACHA20-POLY1305'\n" )

## Mirror selection
# fmtmirr   = "[{:3}] | {:>40}"
# for i in range(len(mirrorlist)):
#     print(fmtmirr.format( i, mirrorlist[i] ))

mirchoice = 0
# while mirchoice < 0 or mirchoice > (len(mirrorlist) - 1):
#     mirchoice = int(input("Select a mirror:: "))

listurl = mirrorlist[mirchoice] + reqpart
response = urllib.request.urlopen(listurl).read().decode('utf-8').split('\n')
reader = csv.reader(response, delimiter=',')

# Get rid of irrelevant headers
reader.__next__()
reader.__next__()

dirpath = os.path.expanduser(ovpndirdir) + "/" + datetime.datetime.now().strftime("%Y-%m-%d_%H_%M")
if not os.path.exists(dirpath):
    os.makedirs(dirpath)

for i in range(serversnum):
    save_ovpn_file(reader, i, dirpath)

#print(fmtstrhead.format( "INDX", "LCTN", "Ping", "Server ID", "Score" ))
#for i in range(serversnum):
#    current = reader.__next__()
#    print(fmtstr.format( i, current[6], current[3], current[0], current[2] ))
#    profiles.append(current[14])
